package cn.xz.tipsum.utils;

import static android.content.Context.ACTIVITY_SERVICE;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import cn.xz.tipsum.utils.MyNotificationListenerService;

public class ServiceUtils {

    /**
     * 判断服务是否运行
     */
    public static boolean isServiceRunning(final String className, AppCompatActivity appCompatActivity) {
        ActivityManager activityManager = (ActivityManager) appCompatActivity.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> info = activityManager.getRunningServices(Integer.MAX_VALUE);
        Log.e("收到--1--",info.toString());
        if (info == null || info.size() == 0) return false;
        for (ActivityManager.RunningServiceInfo aInfo : info) {
            Log.e("收到--2---",aInfo.service.getClassName());
            if (aInfo.service.getClassName().contains(className)) return true;
        }
        return false;
    }


    /**
     * 切换通知监听器服务
     *
     * @param
     */
    public static void toggleNotificationListenerService(AppCompatActivity appCompatActivity) {
        PackageManager pm = appCompatActivity.getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(appCompatActivity, MyNotificationListenerService.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(new ComponentName(appCompatActivity, MyNotificationListenerService.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }


}
